﻿#region

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

#endregion

namespace DataAccessLayer
{
    public static class Booking_Vehicle_DriverDAL
    {
        private static readonly string ConnectionString =
            ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        /// <summary>
        ///     Allocate driver and vehicle for a booking
        /// </summary>
        /// <param name="bvd">Represent's the booking, vehicle, driver</param>
        public static void AllocateVehicleAndDriver(Booking_Vehicle_Driver bvd)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("spAddDriversAndVehiclesForBooking", connection)
                {
                    CommandType = CommandType.StoredProcedure
                };

                command.Parameters.AddWithValue("@BookingId", bvd.BookingId);
                command.Parameters.AddWithValue("@VehicleRegistrationNo", bvd.VehicleRegistrationNo);
                command.Parameters.AddWithValue("@DriverId", bvd.DriverId);

                connection.Open();
                command.ExecuteNonQuery();
            }
        }

        /// <summary>
        ///     Get vehicle and driver details by booking id
        /// </summary>
        /// <param name="bookingId">Represent's the booking Id</param>
        /// <returns>Booking_Vehicle_Driver</returns>
        public static List<Booking_Vehicle_Driver> GetVehicleAndDriverDetailsByBookingId(int bookingId)
        {
            List<Booking_Vehicle_Driver> bvds = new List<Booking_Vehicle_Driver>();

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("spGetDriverAndVehicleDetailsByBookingId", connection)
                {
                    CommandType = CommandType.StoredProcedure
                };

                command.Parameters.AddWithValue("@BookingId", bookingId);

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Booking_Vehicle_Driver bvd = new Booking_Vehicle_Driver
                        {
                            BookingId = bookingId,
                            VehicleRegistrationNo = reader["VehicleRegNo"].ToString(),
                            DriverId = Convert.ToInt32(reader["DriverId"])
                        };

                        bvds.Add(bvd);
                    }
                }
            }
            return bvds;
        }
    }
}